/*******************************************************************************
 *  Copyright (C) 2017 iQIYI Holdings, Inc.
 *  All Rights Reserved.
 *
 *  This file is part of cloud service. Unauthorized copy of this file, via any
 *  medium is strictly prohibited. Proprietary and Confidential.
 *
 *  Author(s):
 *      Xiaoshuang LU <luxiaoshuang@qiyi.com>
*******************************************************************************/

package server;

import iqiyi.solar.common.SolarConstants;
import iqiyi.solar.registry.zookeeper.ZookeeperRegistryClient;
import iqiyi.solar.registry.zookeeper.ZookeeperRegistryConfig;
import iqiyi.solar.server.http.servlet3.MetricsFilter;
import iqiyi.solar.server.http.servlet3.ServletHTTPServer;
import iqiyi.solar.server.http.servlet3.ServletHTTPServerConfig;
import iqiyi.solar.server.http.servlet3.SolarServletContextListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextListener;

@Configuration
public class YetAnotherConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(YetAnotherConfiguration.class);

    @Bean
    public ServletHTTPServerConfig serverConfig() {
        ServletHTTPServerConfig serverConfig = new ServletHTTPServerConfig();

        serverConfig.setProjectID(String.valueOf(SolarConstants.SOLAR_PROJECT_ID));
        serverConfig.setServiceName("HTTPExample");
        serverConfig.setProfile("PROD");
        serverConfig.setPort(8080);

        return serverConfig;
    }

    @Bean
    public ZookeeperRegistryConfig registryConfig() {
        ZookeeperRegistryConfig registryConfig = new ZookeeperRegistryConfig();

        registryConfig.setConnectionString("zk://BJDX");
        registryConfig.setServiceName(String.valueOf(SolarConstants.SOLAR_PROJECT_ID) + "/" + "HTTPExample");
        registryConfig.setRegistryNamespace("servers");

        return registryConfig;
    }

    @Bean
    public ZookeeperRegistryClient registryClient(ZookeeperRegistryConfig registryConfig) {
        ZookeeperRegistryClient registryClient = new ZookeeperRegistryClient(registryConfig);

        return registryClient;
    }

    @Bean
    public ServletHTTPServer solarServer(ServletHTTPServerConfig serverConfig, ZookeeperRegistryClient registryClient) {
        ServletHTTPServer solarServer = new ServletHTTPServer();

        solarServer.setConfig(serverConfig);
        solarServer.setRegistry(registryClient);

        return solarServer;
    }

    @Bean
    public ServletListenerRegistrationBean solarServletContextListener() {
        SolarServletContextListener solarServletContextListener = new SolarServletContextListener();

        ServletListenerRegistrationBean<ServletContextListener> servletListenerRegistrationBean =
            new ServletListenerRegistrationBean<ServletContextListener>();
        servletListenerRegistrationBean.setListener(solarServletContextListener);
        servletListenerRegistrationBean.setOrder(0);
        return servletListenerRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean metricsFilter() {
        MetricsFilter metricsFilter = new MetricsFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(metricsFilter);
        filterRegistrationBean.addInitParameter("all men must die.", "all men must die.");
        filterRegistrationBean.setOrder(0);
        return filterRegistrationBean;
    }
}
